package hyo.hun.lifemanager.today;

import hyo.hun.lifemanager.CalendarSelectDialogActivity;
import hyo.hun.lifemanager.R;
import hyo.hun.lifemanager.common.DBQuery;
import hyo.hun.lifemanager.common.DBSQLite;
import hyo.hun.lifemanager.common.SuperActivity;
import hyo.hun.lifemanager.common.SuperInterface;
import hyo.hun.lifemanager.connect.CalendarConnect;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class TodayActivity extends SuperActivity implements SuperInterface {

	private DBSQLite dbSql;
	private DBQuery dbQuery;
	
	private ListView listviewToday;
	private Button btnAdd;
	
	private View viewResult;
	
	private ArrayList<TodayData> listToday;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.today_activity);
		setTitle(getString(R.string.today_activity_title));
		
		dbSql = DBSQLite.getInstance(superContext);
		dbQuery = new DBQuery(dbSql, superContext);
		
		setFindView();
		setClickListener();
		
		Intent intent = new Intent(superContext, CalendarSelectDialogActivity.class);
		startActivityForResult(intent, 0);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		new ShowDialog().execute(0);
	}
	
	@Override
	public void setFindView() {
		listviewToday = (ListView)findViewById(R.id.today_activity_listview);
		btnAdd = (Button)findViewById(R.id.today_activity_btn_add);
		
		viewResult = getLayoutInflater().inflate(R.layout.view_no_result, null);
		
	}

	@Override
	public void setDataSetting() {
		
		listToday = dbQuery.getTodayTableData();
		
//		for (int i=0; i<10; i++)
//		{
//			TodayData data = new TodayData();
//			data.setId(i+"");
//			listToday.add(data);
//		}
	}

	@Override
	public void setClickListener() {
		
		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				HashMap<String, String> insertMap = new HashMap<String, String>();
				insertMap.put("text", "content");
				
				try
				{
					dbSql.beginTansaction(superContext);
					
					dbSql.insert(superContext, insertMap, "today");
					
					dbSql.setTransactionSuccessful();
				} catch (Exception e)
				{
					// 에러 처리
				} finally 
				{
					try
					{
						dbSql.endTransaction(superContext);
					} catch (Exception e)
					{
						// 에러 처리
					}
				}
				new ShowDialog().execute(0);
			}
		});	
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		;
		if (resultCode == Activity.RESULT_OK)
		{
			String selectData = data.getStringExtra("asdf");
			Log.e("123123", selectData);
			CalendarConnect.getCalendarConnect().loadEvents(selectData, superContext);
		}
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3)
		{
			TodayData data = listToday.get(position);
			
			if (data.getId().equals("-1"))
			{
				return;
			}
			
			Intent intent = new Intent(superContext, TodayDialogActivity.class);
			intent.putExtra("Asdf", data);
			startActivity(intent);
		}
	};
	
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(superContext);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				setDataSetting();
				return 0;
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				if (listviewToday.getFooterViewsCount() > 0)
				{
					listviewToday.removeFooterView(viewResult);
				}
				
				if (listToday.size() < 1)
				{
					listviewToday.addFooterView(viewResult);
					listviewToday.setFooterDividersEnabled(false);
					listviewToday.setOnItemClickListener(null);
				}
				else
				{
					listviewToday.setSelected(true);
					listviewToday.setOnItemClickListener(onItemClickListener);
					listviewToday.setFooterDividersEnabled(true);
				}
				
				TodayListAdapter adapter = new TodayListAdapter(superContext, R.layout.today_activity_list_item, listToday);
				listviewToday.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
		}
	}
}
